package cn.stylefeng.guns.modular.business.frequency.orderextrafee.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.entity.OrderExtraFee;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.mapper.OrderExtraFeeMapper;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.model.param.OrderExtraFeeParam;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.service.OrderExtraFeeService;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.enums.OrderExtraFeeExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单其他额外费用表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderExtraFeeServiceImpl extends ServiceImpl<OrderExtraFeeMapper, OrderExtraFee> implements OrderExtraFeeService {

    @Override
    public PageResult<OrderExtraFee> page(OrderExtraFeeParam orderExtraFeeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderExtraFee> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderExtraFee> list(OrderExtraFeeParam orderExtraFeeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderExtraFee> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderExtraFeeParam orderExtraFeeParam) {

        // 将dto转为实体
        OrderExtraFee orderExtraFee = new OrderExtraFee();
        BeanUtil.copyProperties(orderExtraFeeParam, orderExtraFee);

        this.save(orderExtraFee);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderExtraFeeParam orderExtraFeeParam) {
        this.removeById(orderExtraFeeParam.getId());
    }

    @Override
    public void edit(OrderExtraFeeParam orderExtraFeeParam) {

        // 根据id查询实体
        OrderExtraFee orderExtraFee = this.queryOrderExtraFee(orderExtraFeeParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderExtraFeeParam, orderExtraFee);

        this.updateById(orderExtraFee);
    }

    @Override
    public OrderExtraFee detail(OrderExtraFeeParam orderExtraFeeParam) {
        return this.queryOrderExtraFee(orderExtraFeeParam);
    }



    /**
     * 获取订单其他额外费用表
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderExtraFee queryOrderExtraFee(OrderExtraFeeParam orderExtraFeeParam) {
        OrderExtraFee orderExtraFee = this.getById(orderExtraFeeParam.getId());
        if (ObjectUtil.isEmpty(orderExtraFee)) {
            throw new ServiceException(OrderExtraFeeExceptionEnum.NOT_EXISTED);
        }
        return orderExtraFee;
    }
}